package com.lvhr.rpc.serializer;

import java.io.*;

public class JdkSerializer implements Serializer {


    /**
     * 序列化
     * @param object
     * @param <T>
     * @return
     * @throws IOException
     */
    @Override
    public <T> byte[] serialize(T object) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        try {
            objectOutputStream.writeObject(object);
            objectOutputStream.flush();//数据完全写入确保
            return outputStream.toByteArray();
        } finally {
            objectOutputStream.close();
        }

    }

    /**
     * 反序列化
     * @param bytes 字节数组
     * @param type 目标类型
     * @param <T>
     * @return
     * @throws IOException
     */
//    @Override
//    public <T> T deSerialize(byte[] bytes, Class<T> type) throws IOException {
//        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
//        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
//
//        try {
//            return (T) objectInputStream.readObject();
//        }
//        catch (ClassNotFoundException e){
//            throw new RuntimeException(e);
//        }
//        finally {
//            objectInputStream.close();
//        }
//    }
    @Override
    public <T> T deserialize(byte[] bytes, Class<T> type) throws IOException {
        // 1. 校验输入数据完整性
        if (bytes == null || bytes.length == 0) {
            throw new IllegalArgumentException("Input byte array is null or empty"); 
        }

        // 2. 使用 try-with-resources 确保流关闭（尽管 ByteArrayInputStream 无需关闭，但保持规范）
        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
             ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {

            // 3. 直接读取对象并校验类型
            Object obj = objectInputStream.readObject();
            if (type.isInstance(obj)) {
                return type.cast(obj);
            } else {
                throw new ClassCastException("Deserialized object type mismatch. Expected: " + type);
            }

        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Class not found during deserialization", e);
        } catch (EOFException e) {
            throw new IOException("Incomplete data stream: Failed to deserialize object", e);
        }
    }

/*    @Override
    public <T> T deSerialize(byte[] bytes, Class<T> type) throws IOException, ClassNotFoundException {
        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
             ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
            Object deserialized = objectInputStream.readObject();
            if (!type.isInstance(deserialized)) {
                throw new ClassCastException("Deserialized object is not of type " + type.getName());
            }
            return type.cast(deserialized);
        }
    }*/

}
